<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Posts extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('Posts_model', '', TRUE);
        $this->load->model('Users_model', '', TRUE);
        $this->load->driver('minify');
    }

    /**
     * 列表
     */
    public function index() {
        $this->load->library('pagination');

        $config['base_url'] = '/posts/index';
        $config['total_rows'] = $this->Posts_model->get_count_all();
        $config['per_page'] = 20;
        $config['uri_segment'] = 3;

        $this->pagination->initialize($config);

        $data['posts'] = $this->Posts_model->get_posts(
                $config['per_page'], 
                $this->uri->segment(3)
         );
        $data['page_title'] = 'welcome to ouyoyo`s blog';
        $data['page_description'] = '欢迎来到 ouyoyo,此程序由 Codeigniter 驱动的开源博客程序。在 BSD开源协议 下发布。 前端采用 960 Grid System 布局， HTML5编码。 拥有文章发布，微博同步，评论，相册等功能。';
        $data['page_keywords'] = 'blog, Codeigniter, 960 Grid System, html5, jquery';

        $this->load->view('articles/index', $data);
    }

    /**
     * 详细页面
     */
    public function view($id) {
        
        $post = $this->Posts_model->get_post_by_id($id);

        $this->load->library('pagination');
        $this->load->model('Comments_model', '', TRUE);
        $this->load->helper('gravatar'); 

        $data['post_pre'] = $this->Posts_model->get_pre_post($id);
        $data['post_next'] = $this->Posts_model->get_next_post($id);
        $data['page_title'] = $post->post_title;
        $data['page_description'] = $this->Posts_model->get_post_description($post->post_content_html);
        $data['post'] = $post;
        $data['dtype'] = 1;
        $data['did'] = $post->ID;
        $data['page_keywords'] = 'keyworkds';

        $config['base_url'] = '/articles/' . $post->ID . '/page/';
        $config['total_rows'] = $post->comment_count;
        $config['per_page'] = 12;
        $config['uri_segment'] = 5;
        $this->pagination->initialize($config);
        $data['comments'] = $this->Comments_model->get_comments($post->ID, 1, $config['per_page'], $this->uri->segment(5));

        $this->load->view('articles/view', $data);
    }

    /**
     * 更新文章
     * 
     */
    public function update($id) {
        $this->Users_model->is_logged_in();

        $post = $this->Posts_model->get_post_by_id($id);
        
        $data['page_title'] = '更新文章';
        $data['post'] = $post;
        $data['page_description'] = '更新文章';
        $data['page_keywords'] = '更新文章';
        $this->load->view('articles/update', $data);
    }

    /**
     * 更新
     */
    public function edit_post() {
        $this->Users_model->is_logged_in();

        $this->load->library('form_validation');
        $this->form_validation->set_rules('post_title', 'post_title', 'trim|required');
        $this->form_validation->set_rules('post_content', 'post_content	', 'trim|required');

        if ($this->form_validation->run() != FALSE) {
            if ($this->input->post('ID')) {
                $this->Posts_model->update($this->input->post('ID'));
                $query = $this->input->post('ID');
            } else {
                $query = $this->Posts_model->create();
            }
            redirect('articles/' . $query);
        } else {
            redirect('posts/create');
        }
    }

    /**
     * 添加
     */
    public function create() {
        $this->Users_model->is_logged_in();

        $data['page_title'] = '添加文章';
        $data['page_description'] = '添加文章';
        $data['page_keywords'] = '添加文章';

        $this->load->view('articles/create', $data);
    }

    /**
     * 上传图片
     */
    public function upload_image() {
        $this->load->model('Images_model', '', TRUE);
        $data['image'] = $this->Images_model->upload_image();

        $this->load->view('articles/upload_image', $data);
    }

}